package com.example.application.views.regiest;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.example.application.bean.ResultBean;
import com.example.application.bean.User;
import com.example.application.config.URLConfig;
import com.example.application.views.util.DialogUtil;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.vaadin.flow.component.UI;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.gridpro.GridPro;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.router.PageTitle;
import com.example.application.views.main.MainView;
import com.vaadin.flow.component.dependency.CssImport;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

@Route(value = "registList", layout = MainView.class)
@PageTitle("申请列表")
@CssImport("./views/申请列表/申请列表-view.css")
public class RegiestView extends Div {

    private GridPro<User> grid;

    List<User> users;

    public RegiestView(){
        User user = (User) UI.getCurrent().getSession().getAttribute("user");
        if (user == null){

        }else if("平台管理员".equals(user.getIdentity())){
            this.init();
        }else{
            DialogUtil.adminAuthority();
        }
    }

    public Button saveMsg(GridPro<User> grid, User item){
        @SuppressWarnings("unchecked")
        Button button = new Button("确定", e->{
//            保存修改后的方法
            String url = URLConfig.SAVE_USER + "/" + item.getState() + "/" + item.getId();

            ObjectMapper mapper = new ObjectMapper();

            ResultBean<String> resultBean =null;
            try (CloseableHttpClient httpClient = HttpClients.createDefault()){

                HttpPut httpPut = new HttpPut(url);
                CloseableHttpResponse httpResponse = httpClient.execute(httpPut);

                String json = EntityUtils.toString(httpResponse.getEntity(), "utf-8");

                JavaType javaType = mapper.getTypeFactory().constructParametricType(ResultBean.class, String.class);

                resultBean = mapper.readValue(json, javaType);

                String isUpdate = "";
                if(resultBean.getCode() == 1){
                    isUpdate = "alert('修改成功');";
                }else{
                    isUpdate = "alert('修改失败');";
                }
                UI.getCurrent().getPage().executeJs(isUpdate);
                updateGrid();
            } catch (IOException exception) {
                exception.printStackTrace();
            }
        });

        return button;
    }

    public void updateGrid(){
        users = new ArrayList<>();
        String url = URLConfig.USER_REGIESTR;

        ObjectMapper mapper = new ObjectMapper();

        ResultBean<User[]> resultBean =null;
        try (CloseableHttpClient httpClient = HttpClients.createDefault()){

            HttpGet httpGet = new HttpGet(url);
            CloseableHttpResponse httpResponse = httpClient.execute(httpGet);

            String json = EntityUtils.toString(httpResponse.getEntity(), "utf-8");

            JavaType javaType = mapper.getTypeFactory().constructParametricType(ResultBean.class, User[].class);

            resultBean = mapper.readValue(json, javaType);
//            System.out.println(resultBean);

        } catch (IOException exception) {
            exception.printStackTrace();
        }

       User[] user1 = resultBean.getT();
        if(user1 != null){
            for (User user : resultBean.getT()) {
                users.add(user);
            }
        }

        grid.setItems(users);
    }

    public void init(){
        grid = new GridPro<>();

        grid.addColumn(User::getLoginName).setHeader("用户名").setAutoWidth(true);
        grid.addColumn(User::getName).setHeader("姓名").setAutoWidth(true);
        grid.addColumn(User::getEmail).setHeader("邮箱").setAutoWidth(true);
        grid.addColumn(User::getTelNum).setHeader("电话").setAutoWidth(true);
        grid.addColumn(User::getIdentity).setHeader("身份").setAutoWidth(true);
        List<String> optionsList = new ArrayList<>();
        optionsList.add("待审核");
        optionsList.add("可用");
        optionsList.add("不可用");
        grid.addEditColumn(User::getState).select(User::setState, optionsList).setHeader("状态");
        grid.addComponentColumn(item -> saveMsg(grid, item)).setHeader("操作");
        updateGrid();
        grid.setItems(users);
        add(grid);
    }

}
